<template>
  <div v-show="hidden" class="tabbar van-hairline--top">
    <ul class="list">
      <router-link  
        v-for="item in tabbars"
        :key="item.path"
        :to="item.path"
        class="list-item active"
      >
        <van-icon 
          v-if="isCart && item.meta.icon === 'cart-o'" 
          :name="item.meta.icon" 
          :badge="!cartStore.count ? '' : cartStore.count" 
        />
        <van-icon v-else :name="item.meta.icon" />
        <span>{{item.meta.title}}</span>
      </router-link>
    </ul>
  </div>
</template>

<script setup>
import {ref, watch} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import useCartStore from '@/store/shopcart'
import {generateTabbar, filterTabbar} from '@/utils/route'

const route = useRoute()
const router = useRouter()
const cartStore = useCartStore()

const hidden = ref(false)
const isCart = ref(false)
const tabbars = generateTabbar(router.getRoutes())
const tabWhitelist = filterTabbar(tabbars)

watch(
  () => route.path,
  newPath => {
    if (tabWhitelist.includes(newPath)) {
      hidden.value = true
    } else {
      hidden.value = false
    }
    if (newPath === '/shopcart') isCart.value = true
    else isCart.value = false
  },
  {immediate: true}
)

</script>

<style lang="less" scoped>
@import '../assets/style/mixin.less';

.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 5px 0;
  z-index: 1000;
  background: #fff;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  .list {
    width: 100%;
    .fj();
    flex-direction: row;
    padding: 0;
    .list-item {
      display: flex;
      flex: 1;
      // width: 25%;
      // justify-content: center;
      flex-direction: column;
      text-align: center;
      color: #666;
      &.router-link-active {
        color: @primary;
      }
      i {
        text-align: center;
        font-size: 22px;
      }
      span{
        font-size: 12px;
      }
      .van-icon-shopping-cart-o {
        margin: 0 auto;
        margin-bottom: 2px;
      }
    }
  }
}
:deep(.van-badge--top-right) {
  top: 5px;
  right: 20px;
  transform: translate(0, -50%) !important;
}
</style>